package com.swfee.qinben.adpt;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.swfee.qinben.R;
import com.swfee.qinben.dto.rsp.ScheduleDayRspDto;
import com.swfee.qinben.session.SystemConfig;
import com.swfee.qinben.util.CalendarUtil;

public class CalendarGridViewAdapter extends BaseAdapter {

	private Calendar calStartDate = Calendar.getInstance();// 当前显示的日历
	private Calendar calSelected = Calendar.getInstance(); // 选择的日历
	private List<ScheduleDayRspDto> scheduleDays = null;
	private SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy/M/d");
	private int compareIndex = 0;

	public void setSelectedDate(Calendar cal) {
		calSelected = cal;
	}

	private Calendar calToday = Calendar.getInstance(); // 今日
	private int iMonthViewCurrentMonth = 0; // 当前视图月
	ArrayList<java.util.Date> titles;
	private Activity activity;
	Resources resources;

	// 根据改变的日期更新日历
	// 填充日历控件用
	private void UpdateStartDateForMonth() {
		calStartDate.set(Calendar.DATE, 1); // 设置成当月第一天
		iMonthViewCurrentMonth = calStartDate.get(Calendar.MONTH);// 得到当前日历显示的月

		// 星期一是2 星期天是1 填充剩余天数
		int iDay = 0;
		int iFirstDayOfWeek = Calendar.MONDAY;
		int iStartDay = iFirstDayOfWeek;
		if (iStartDay == Calendar.MONDAY) {
			iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY;
			if (iDay < 0)
				iDay = 6;
		}
		if (iStartDay == Calendar.SUNDAY) {
			iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
			if (iDay < 0)
				iDay = 6;
		}
		calStartDate.add(Calendar.DAY_OF_WEEK, -iDay);
		calStartDate.add(Calendar.DAY_OF_MONTH, SystemConfig.weekFirstDay);// 周日第一位
																			// //-1为周一第一位

	}

	private ArrayList<java.util.Date> getDates() {

		UpdateStartDateForMonth();

		ArrayList<java.util.Date> alArrayList = new ArrayList<java.util.Date>();

		for (int i = 1; i <= 42; i++) {
			alArrayList.add(calStartDate.getTime());
			calStartDate.add(Calendar.DAY_OF_MONTH, 1);
		}

		return alArrayList;
	}

	// construct
	public CalendarGridViewAdapter(Activity a, Calendar cal,
			List<ScheduleDayRspDto> scheduleDays) {
		calStartDate = cal;
		activity = a;
		resources = activity.getResources();
		titles = getDates();

		// String[] arrays =
		// "2013/3/15,2013/3/18,2013/3/19,2013/3/25,2013/3/28,2013/3/31,2013/4/6,2013/5/1"
		// .split(",");
		this.scheduleDays = scheduleDays;

	}

	@Override
	public int getCount() {
		return titles.size();
	}

	@Override
	public Object getItem(int position) {
		return titles.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater mInflater = (LayoutInflater) activity
				.getLayoutInflater();

		LinearLayout iv = (LinearLayout) mInflater.inflate(R.layout.days, null);

		iv.setId(position + 5000);
		iv.setOrientation(1);
		Date myDate = (Date) getItem(position);
		String dateStr = dateFmt.format(myDate);
		iv.setTag(myDate);
		Calendar calCalendar = Calendar.getInstance();
		calCalendar.setTime(myDate);

		// 初始色彩
		int colorGridView = R.color.main_bg;
		int colorNlTxt = R.color.chineseNl;
		int colorGlTxt = R.color.monthDay;

		final int iMonth = calCalendar.get(Calendar.MONTH);
		final int iDay = calCalendar.get(Calendar.DAY_OF_WEEK);
		TextView nlDayTxv = (TextView) iv.findViewById(R.id.nlTxt);
		TextView glDayTxv = (TextView) iv.findViewById(R.id.glTxt);
		;// 日期

		int day = myDate.getDate(); // 日期
		glDayTxv.setText(String.valueOf(day));
		glDayTxv.setId(position + 500);

		CalendarUtil calendarUtil = new CalendarUtil(calCalendar);
		nlDayTxv.setText(calendarUtil.toString());
		boolean isToday = false;

		// 这里用于比对是不是比当前日期
		int compValue = CalendarUtil.compare(myDate, calToday.getTime());

		// 判断是否是当前月
		if (iMonth == iMonthViewCurrentMonth) {

			switch (compValue) {
			case 0:
				isToday = true;
				break;
			case 1:
			case -1:
				colorGlTxt = R.color.black;
				break;
			}

			// 判断周六周日
			if (iDay == 7 || iDay == 1) {
				colorGlTxt = R.color.weekedYellow;

			}
		} else {
			colorGlTxt = R.color.notNonthDay;
		}

		if (isToday) {
			iv.findViewById(R.id.todayLayOut).setBackgroundDrawable(
					resources.getDrawable(R.drawable.bg_cur_day));
		}

		if (calendarUtil.isFestivalDay()) {
			colorNlTxt = R.color.festivalDay;
		}

		if (scheduleDays != null) {
			for (int i = compareIndex; i < scheduleDays.size(); i++) {

				if (scheduleDays.get(i).getDate().equals(dateStr)) {
					if (compValue <= -1) {
						colorGridView = R.color.scheduleDayBefor;
					} else {
						colorGridView = R.color.scheduleDayAfter;
					}
				}

			}
		}

		if (equalsDate(calSelected.getTime(), myDate)) { // 选择的
			colorGridView = R.color.dateSelected;
		}

		// 设置背景颜色结束

		nlDayTxv.setTextColor(resources.getColor(colorNlTxt));
		glDayTxv.setTextColor(resources.getColor(colorGlTxt));
		iv.setBackgroundColor(resources.getColor(colorGridView));

		return iv;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}

	private Boolean equalsDate(Date date1, Date date2) {

		if (date1.getYear() == date2.getYear()
				&& date1.getMonth() == date2.getMonth()
				&& date1.getDate() == date2.getDate()) {
			return true;
		} else {
			return false;
		}

	}

}
